package com.untrance.shop.core.builder.vo;

import com.untrance.shop.common.vo.ProductVO;
import com.untrance.shop.model.Product;

import java.text.DecimalFormat;

/**
 * @author eugene zadyra
 */
public class ProductVOBuilder extends AbstractVOBuilder<Product, ProductVO> {

	public ProductVOBuilder() {

	}

	@Override
	protected ProductVO transform(Product product) {
		ProductVO vo = new ProductVO();
		if (product.getAvailability() != null) {
			vo.setAvailable(product.getAvailability().name());
		}
		vo.setImageName(product.getImageName());
		vo.setName(product.getName());
		if (product.getNewPrice() != null) {
			vo.setNewPrice(priceFormat(product.getNewPrice()));
		}
		if (product.getOldPrice() != null) {
			vo.setNewPrice(priceFormat(product.getOldPrice()));
		}
		vo.setShortDescription(product.getShortDescription());
		return vo;
	}

	private String priceFormat(double value) {
		DecimalFormat myFormatter = new DecimalFormat("#.00");
		return myFormatter.format(value);
	}

}
